<?php

require_once "Utils.php";

class HuJiang
{

    /**
     * @param string $html 网页源代码
     * @return bool true表示找到
     */
    private static function Isfind($html)
    {
        $pattern = '#对不起，没有查询到您要的内容#';
        return !preg_match($pattern, $html, $matchs);
    }

    /**
     * @param string $url 网址
     * @return string 网址的源码
     */
    private static function curlGetHtml($url)
    {
        //1.初始化，创建一个新cURL资源
        $ch = curl_init();
        //2.设置URL和相应的选项
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        //3.抓取URL并把它传递给浏览器
        $output = curl_exec($ch);


        //4.关闭cURL资源，并且释放系统资源
        curl_close($ch);

        return $output;
    }

    /**
     * @param string $word 查询的单词
     * @param mixed $wordTable 要插入的数据表
     * @return array|bool 查到返回数据，没找到返回false
     */
    public static function getFromHuJiang($word)
    {

        $type = "jc";
        $url = "http://dict.hjenglish.com/services/huaci/jp_web_ajax.ashx?type={$type}&w={$word}";
        $html = self::curlGetHtml($url);

        //unicode解码
        $html = Utils::decodeUnicode($html);

        if (self::Isfind($html)) {

            //将制表符\t置换掉
            $html = str_replace('\t', '', $html);
            //去掉反义符\
            $html = stripslashes($html);

            //echo $html . "<br>";

            //使用phpQuery进行文档解析
            require('phpQuery/phpQuery.php');

            $doc = phpQuery::newDocumentHTML($html);
            phpQuery::selectDocument($doc);

            //翻译
            $fanyi = pq("#com_panel_0")->text();
            //MP3链接
            $mp3link = pq('#hjd_jp_pronounce_sound')->eq(0)->text();

            //eq(0)是选择第一个元素
            $infoObj = pq('span.hjd_jp_title.hjd_fl')->eq(0);

            //假名
            $kana = $infoObj->find('span[title="假名"]')->text();

            //罗马音
            $luoma = $infoObj->find('span[title="罗马音"]')->text();

            //声调
            $sheng = $infoObj->find('span[title="声调"]')->text();
            $data = [];
            $data['word'] = $word;
            $data['kana'] = $kana;
            $data['jieshi'] = $fanyi;
            $data['luoma'] = $luoma;
            $data['sheng'] = $sheng;
            $data['mp3'] = $mp3link;

            return $data;
        } else {

        }

        return false;
        //echo $kana, $luoma, $sheng, $fanyi, $mp3link;
    }

    /**
     * 查询单词，数据库没有的话会从沪江官网下载
     * @param $word
     * @return array|bool|mixed
     */
    public static function findWord($word)
    {
        include_once "Tabel.php";

        //去掉前后空格
        $word = trim($word);
        //去掉中间的空格
        $word = preg_replace('# #', '', $word);

        // 创建实体对象
        $wordTable = new Table("word", "u");

        // 开启调试模式
        //$wordTable->debug = true;

        // sql查询
        $sql = "SELECT * FROM word WHERE word=?";
        //获取一行数据
        $row = $wordTable->query($sql, $word)->fetch();

        if ($row) {
            return $row;
        } else {
            $row = self::getFromHuJiang($word, $wordTable);
            //从沪江获得数据，插入数据库
            if ($row) {
                $wordTable->insertReplace($row)->rowCount();
            }
        }

        //var_dump($wordTable->vquery($sql, $word));
        return $row;
    }
}